package com.wkck.controller.frontend;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.wkck.VO.UserAllInfoVo;
import com.wkck.VO.UserDeliveredCareersVo;
import com.wkck.context.BaseContext;
import com.wkck.domain.DeliveredLibraryHistory;
import com.wkck.domain.User;
import com.wkck.domain.UserElseInfo;
import com.wkck.dto.UserLoginDto;
import com.wkck.result.Result;
import com.wkck.service.UserFrontendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: CK
 * @Date: 2024/02/28/18:09
 * @Description:
 */
@Api(value = "用户Api", tags = "用户api")
@Slf4j
@RestController
@RequestMapping("/user/user")
@CrossOrigin
public class UserFrontendController {

    @Autowired
    private UserFrontendService userFrontendService;

    /***
    * @Author: CK
    * @Date: 2024/2/28
    * @Param: [userLoginDto]
    * @Description: 用户登录及注册
    */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<Map<String,Object>> userLogin(@RequestBody UserLoginDto userLoginDto, HttpSession httpSession) {
        log.info("登录信息: {}", userLoginDto);
        return Result.success(userFrontendService.login(userLoginDto,httpSession));
    }

    /**
    * @Author: CK
    * @Date: 2024/2/29
    * @Param: [id]
    * @Description: 根据用户id获得信息
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据用户id获得信息")
    public Result<UserAllInfoVo> getUserAllInfoById(@PathVariable("id") Integer id) {
        log.info("userId =====> {} ", id);
        return Result.success(userFrontendService.getUserAllInfoById(id));
    }

    /***
    * @Author: CK
    * @Date: 2024/2/29
    * @Param: [id, user]
    * @Description: 修改用户基础信息
    */
    @PostMapping("/base/{id}")
    @ApiOperation(value = "修改用户基础信息")
    public Result<Boolean> modifyUserBaseInfo(@PathVariable("id") Integer id, @RequestBody User user) {
        log.info("需要修改的用户信息: {}, {}", id, user);
        return Result.success(userFrontendService.modifyUserBaseInfo(id, user));
    }

    /***
     * @Author: CK
     * @Date: 2024/2/29
     * @Param: [id, user]
     * @Description: 修改用户基础信息
     */
    @PostMapping("/else/{id}")
    @ApiOperation(value = "修改用户其他信息")
    public Result<Boolean> modifyUserElseInfo(@PathVariable("id") Integer id, @RequestBody UserElseInfo userElseInfo) {
        log.info("需要修改的用户信息: {}, {}", id, userElseInfo);
        return Result.success(userFrontendService.modifyUserElseInfo(id, userElseInfo));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/19
    * @Param: [id]
    * @Description: 根据用户id查询用户已投递的职位
    */
    @GetMapping("/deliver/{user_id}")
    @ApiOperation(value = "根据用户id查询用户已投递的职位")
    public Result<List<UserDeliveredCareersVo>> queryUserDeliveredCareersByUserId(@PathVariable("user_id") Integer id) {
        log.info("根据用户id ==> {}, 查询用户已投递的职位", id);
        return Result.success(userFrontendService.queryUserDeliveredCareersByUserId(id));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/19
    * @Param: [dl_id]
    * @Description: 根据投递id获取投递历史记录
    */
    @GetMapping("/history/{dl_id}")
    @ApiOperation(value = "根据投递id获取投递历史记录")
    public Result<List<DeliveredLibraryHistory>> getDeliverHistoryById(@PathVariable Long dl_id) {
        log.info("根据投递id ===> {}, 获取投递历史记录", dl_id);
        return Result.success(userFrontendService.getDeliverHistoryById(dl_id));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/19
    * @Param: [dl_id]
    * @Description: 撤回投递记录
    */
    @DeleteMapping("/deliver/remove/{dl_id}")
    @ApiOperation(value = "撤回投递记录")
    public Result<Boolean> withdrawDeliverHistory(@PathVariable Long dl_id) {
        log.info("撤回投递记录 ===> {}", dl_id);
        return Result.success(userFrontendService.withdrawDeliverHistory(dl_id));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/20
    * @Param: [file]
    * @Description: 修改用户头像
    */
    @PostMapping("/avatar/change")
    @ApiOperation(value = "修改用户头像")
    public Result<Boolean> changeUserAvatarById(@RequestPart MultipartFile file) throws IOException {
        int id = BaseContext.getCurrentId().intValue();
        log.info("更改头像的用户: {}", id);

        File tempFile = File.createTempFile("minio_user_avatar", ".temp");
        file.transferTo(tempFile);

        String localFilePath = tempFile.getAbsolutePath();

        String filename = file.getOriginalFilename();

        return Result.success(userFrontendService.changeUserAvatarById(id, localFilePath, filename));
    }
}
